 package cn.org.eagle_framework.generator;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import cn.org.eagle_framework.generator.provider.db.DataSourceProvider;
 
/**
 * @Copyright @ 2013
 * All right reserved
 * @version 创建时间：Created on 2013-3-7
 * @author 作者：Create by www.360buyli.com
 * @Email: 360buyli@gmail.com
 * @description 用于插入模块与操作的数据
 */

public class DataBiz {
	
	//添加菜单数据
	public static void appendMenuData(GeneratorBiz biz){
		//删除原来的数据
		StringBuffer menuName=new StringBuffer();
		String[] packageNames=biz.getModelNames();
		
		String listAction="/"+biz.getClassName()+"toList.action";
		String insertAction=biz.getClassName()+"toInsert.action";
		String deleteAction=biz.getClassName()+"doDelete.action";
		String viewAction=biz.getClassName()+"toView.action";
		String updateAction=biz.getClassName()+"toUpdate.action";
		for (String packageName : packageNames) {
			menuName.append(packageName).append(".");
		}
		
		String menuDeleteSql="delete from sys_module where moduleidf='"+menuName.toString().substring(0, menuName.length()-1)+"'";
		String menuInsertSql="insert into sys_module (moduleidf, namef, urlf, remarkf, parentidf, datasortnumf, statusf) values " +
				"('"+menuName.toString().substring(0, menuName.length()-1)+"', '"+GeneratorBiz.bizName+"', " +
						"'"+listAction+"','', '"+biz.getModelNames()[biz.getModelNames().length-2]+"', "+System.currentTimeMillis()+",0)";
		System.out.println(menuDeleteSql);
		System.out.println(menuInsertSql);
		
		
		String allOperationDeleteSql="delete from sys_module_operation where moduleidf='"+menuName.toString().substring(0, menuName.length()-1)+"'";
		String operationInsertSql="insert into sys_module_operation (operationidf, moduleidf, namef, remarkf, urlf, datasortnumf) values " +
				"('"+menuName+"toInsert"+"', '"+menuName.toString().substring(0, menuName.length()-1)+"', '新增', '', '"+insertAction+"', "+System.currentTimeMillis()+")";
		
		String operationDeleteSql="insert into sys_module_operation (operationidf, moduleidf, namef, remarkf, urlf, datasortnumf) values " +
		"('"+menuName+"doDelete"+"', '"+menuName.toString().substring(0, menuName.length()-1)+"', '删除', '', '"+deleteAction+"', "+System.currentTimeMillis()+")";
		
		String operationViewSql="insert into sys_module_operation (operationidf, moduleidf, namef, remarkf, urlf, datasortnumf) values " +
		"('"+menuName+"toView"+"', '"+menuName.toString().substring(0, menuName.length()-1)+"', '查看', '', '"+viewAction+"', "+System.currentTimeMillis()+")";
		
		String operationUpdateSql="insert into sys_module_operation (operationidf, moduleidf, namef, remarkf, urlf, datasortnumf) values " +
		"('"+menuName+"toUpdate"+"', '"+menuName.toString().substring(0, menuName.length()-1)+"', '修改', '', '"+updateAction+"', "+System.currentTimeMillis()+")";
		Connection con = DataSourceProvider.getConnection();
		try {
			Statement stat = con.createStatement();
			System.out.println(menuDeleteSql);
			stat.execute(menuDeleteSql);
			
			System.out.println(menuInsertSql);
			stat.execute(menuInsertSql);
			
			System.out.println(allOperationDeleteSql);
			stat.execute(allOperationDeleteSql);
			
			System.out.println(operationInsertSql);
			stat.execute(operationInsertSql);
			
			System.out.println(operationDeleteSql);
			stat.execute(operationDeleteSql);
			
			System.out.println(operationViewSql);
			stat.execute(operationViewSql);
			
			System.out.println(operationUpdateSql);
			stat.execute(operationUpdateSql);
			
			
			
			stat.close();
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}

